package com.ac.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ac.model.FileLog;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class FileLogService {

	final MongoTemplate mongo;

	public List<FileLog> list() {
		return mongo.findAll(FileLog.class);
	}

	@Transactional(rollbackFor = Exception.class)
	public void insert(FileLog fileLog) {
		fileLog.insert();
		mongo.insert(fileLog);
	}

	@Transactional(rollbackFor = Exception.class)
	public FileLog info(FileLog fileLog) {
		return mongo.findOne(Query.query(Criteria.where("name").is(fileLog.getName()).and("userId")
				.is(fileLog.getUserId()).and("type").is(fileLog.getType())), FileLog.class);
	}

	@Transactional(rollbackFor = Exception.class)
	public void remove(FileLog fileLog) {
		mongo.remove(fileLog);
	}

	/**
	 * 获取字符串中的src文件id列表
	 * 
	 * @param content
	 * @return
	 */
	private List<String> getFileIds(String content) {
		String regex = "src=\"[^http]([\\s\\S]*?)\"";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(content);
		List<String> resList = new ArrayList<String>();
		while (matcher.find()) {
			String str = matcher.group(1);
			str = str.substring(str.lastIndexOf("/") + 1);
			str = str.substring(0, str.lastIndexOf("."));
			resList.add(str);
		}
		return resList;
	}

	/**
	 * 修改富文本中的文件资源签名
	 * 
	 * @param content
	 */
	@Transactional(rollbackFor = Exception.class)
	public void richTextSign(String content) {
		List<String> resList = getFileIds(content);
		if (resList.size() > 0)
			initSign(resList);
	}

	/**
	 * 修改富文本中的文件资源签名为临时文件
	 * 
	 * @param content
	 */
	@Transactional(rollbackFor = Exception.class)
	public void richTextSignTemp(String content) {
		List<String> resList = getFileIds(content);
		if (resList.size() > 0)
			initSign(resList, FileLog.TempUploadSign);
	}

	/**
	 * 根据id列表修改文件资源签名,使用默认参数
	 * 
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void initSign(List<String> ids) {
		initSign(ids, FileLog.UploadSign);
	}

	/**
	 * 根据id列表修改文件资源签名,指定参数
	 * 
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void initSign(List<String> ids, String sing) {
		mongo.updateMulti(Query.query(Criteria.where("id").in(ids)), new Update().set("sign", sing), FileLog.class);
	}

	/**
	 * 清理过期的临时文件
	 * 
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public List<FileLog> clearUploadFile() {
		long thisTime = System.currentTimeMillis() - 172800000L;// 超时时间为2天
		Criteria ct = Criteria.where("sign").is("TempUpload").and("createTime").lt(thisTime);
		List<FileLog> findList = mongo.find(Query.query(ct), FileLog.class);
		for (FileLog fileLog : findList) {
			String url = fileLog.getUrl();
			File file = new File(url);
			if (file.exists()) {
				file.delete();
			}
			mongo.remove(fileLog);
		}
		return findList;
	}

}